package com.iocollege.tinyframe.mapper.system;

import com.iocollege.tinyframe.common.core.MyMapper;
import com.iocollege.tinyframe.entity.system.SysMenu;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 菜单权限表 Mapper 接口
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-03-22
 */
public interface SysMenuMapper extends MyMapper<SysMenu> {

    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Select("select distinct m.perms\n" +
            "\t\tfrom sys_menu m\n" +
            "\t\t\t left join sys_role_menu rm on m.id = rm.menu_id\n" +
            "\t\t\t left join sys_user_role ur on rm.role_id = ur.role_id\n" +
            "\t\t\t left join sys_role r on r.id = ur.role_id\n" +
            "\t\twhere m.status = '0' and r.status = '0' and ur.user_id = #{userId}")
    List<String> selectMenuPermsByUserId(@Param("userId")Long userId);



    /**
     * 根据用户ID查询菜单
     *
     * @return 菜单列表
     */
    @Select("select distinct m.id, m.parent_id, m.menu_name, m.path, m.component, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time\n" +
            "\t\tfrom sys_menu m where m.menu_type in ('M', 'C') and m.status = 0\n" +
            "\t\torder by m.parent_id, m.order_num")
    public List<SysMenu> selectMenuTreeAll();

    /**
     * 根据用户ID查询菜单
     *
     * @param userId 用户ID
     * @return 菜单列表
     */
    @Select("select distinct m.id, m.parent_id, m.menu_name, m.path, m.component, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time\n" +
            "\t\tfrom sys_menu m\n" +
            "\t\t\t left join sys_role_menu rm on m.id = rm.menu_id\n" +
            "\t\t\t left join sys_user_role ur on rm.role_id = ur.role_id\n" +
            "\t\t\t left join sys_role ro on ur.role_id = ro.id\n" +
            "\t\t\t left join sys_user u on ur.user_id = u.id\n" +
            "\t\twhere u.id = #{userId} and m.menu_type in ('M', 'C') and m.status = 0  AND ro.status = 0\n" +
            "\t\torder by m.parent_id, m.order_num")
    public List<SysMenu> selectMenuTreeByUserId(Long userId);


    /**
     * 校验菜单名称是否唯一
     *
     * @param menuName 菜单名称
     * @param parentId 父菜单ID
     * @return 结果
     */
    @Select("select * from sys_menu where menu_name=#{menuName} and parent_id = #{parentId} limit 1")
    public SysMenu checkMenuNameUnique(@Param("menuName") String menuName, @Param("parentId") Long parentId);




    /**
     * 根据角色ID查询菜单树信息
     *
     * @param roleId 角色ID
     * @param menuCheckStrictly 菜单树选择项是否关联显示
     * @return 选中菜单列表
     */
    @Select("<script>select m.id\n" +
            "\t\tfrom sys_menu m\n" +
            "            left join sys_role_menu rm on m.id = rm.menu_id\n" +
            "        where rm.role_id = #{roleId}\n" +
            "            <if test=\"menuCheckStrictly\">\n" +
            "              and m.id not in (select m.parent_id from sys_menu m inner join sys_role_menu rm on m.id = rm.menu_id and rm.role_id = #{roleId})\n" +
            "            </if>\n" +
            "\t\torder by m.parent_id, m.order_num" +
            "</script>")
    public List<Integer> selectMenuListByRoleId(@Param("roleId") Long roleId, @Param("menuCheckStrictly") boolean menuCheckStrictly);


    /**
     * 是否存在菜单子节点
     *
     * @param menuId 菜单ID
     * @return 结果
     */
    @Select(" select count(1) from sys_menu where parent_id = #{menuId}  ")
    public int hasChildByMenuId(Long menuId);


    /**
     * 根据用户查询系统菜单列表
     *
     * @param menu 菜单信息
     * @return 菜单列表
     */
    @Select("<script>select distinct m.id, m.parent_id, m.menu_name, m.path, m.component, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time\n" +
            "\t\tfrom sys_menu m\n" +
            "\t\tleft join sys_role_menu rm on m.id = rm.menu_id\n" +
            "\t\tleft join sys_user_role ur on rm.role_id = ur.role_id\n" +
            "\t\tleft join sys_role ro on ur.role_id = ro.id\n" +
            "\t\twhere ur.user_id = #{params.userId}\n" +
            "\t\t<if test=\"menuName != null and menuName != ''\">\n" +
            "            AND menu_name like concat('%', #{menuName}, '%')\n" +
            "\t\t</if>\n" +
            "\t\t<if test=\"visible != null and visible != ''\">\n" +
            "            AND visible = #{visible}\n" +
            "\t\t</if>\n" +
            "\t\t<if test=\"status != null and status != ''\">\n" +
            "            AND status = #{status}\n" +
            "\t\t</if>\n" +
            "\t\torder by m.parent_id, m.order_num" +
            "</script>")
    public List<SysMenu> selectMenuListByUserId(SysMenu menu);


    @Select("<script> select  * from sys_menu <where>\n" +
            "\t\t\t<if test=\"menuName != null and menuName != ''\">\n" +
            "\t\t\t\tAND menu_name like concat('%', #{menuName}, '%')\n" +
            "\t\t\t</if>\n" +
            "\t\t\t<if test=\"visible != null and visible != ''\">\n" +
            "\t\t\t\tAND visible = #{visible}\n" +
            "\t\t\t</if>\n" +
            "\t\t\t<if test=\"status != null and status != ''\">\n" +
            "\t\t\t\tAND status = #{status}\n" +
            "\t\t\t</if>\n" +
            "\t\t</where>\n" +
            "\t\torder by parent_id, order_num" +
            "</script>")
    List<SysMenu> selectMenuList(SysMenu menu);
}
